<template>
  <div class="h-full px-4 py-3 rounded-lg">
    <div class="flex justify-between">
      <a-button type="link" class="back" @click="handleBack">
        <template #icon>
          <LeftOutlined />
        </template>
        <span>返回</span>
      </a-button>
      <div class="flex justify-end">
        <a-input
          v-model:value.trim="searchForm.name"
          placeholder="请输入摄像机名称"
          v-filter-special-chars
          maxLength="50"
          class="_input"
        />
        <a-select
          class="_input"
          v-model:value="searchForm.type"
          allow-clear
          placeholder="选择摄像机类型"
          :options="typeList"
        />
        <a-select
          class="_input"
          v-model:value="searchForm.status"
          allow-clear
          placeholder="选择设备状态"
          :options="statusList"
        />
        <SerchButtom @search="getList(true, true)" @reset="reset" />
      </div>
    </div>

    <div class="mt-3 h-[calc(100%-6rem)] overflow-y-auto">
      <a-table
        :dataSource="dataSource"
        :columns="columns"
        :pagination="false"
        :loading="loading"
      >
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'order'">
            {{ (pagination.current - 1) * pagination.pageSize + index + 1 }}
          </template>
          <template v-else-if="column.dataIndex === 'operation'">
            <a-button type="link" @click="openModal('edit', record)">
              修改
            </a-button>
          </template>
        </template>
      </a-table>
    </div>

    <div class="mt-3 h-8">
      <Pagination @getListByPage="getListByPage" ref="paginationRef" />
    </div>
  </div>

  <IndexDetailModal
    ref="indexDetailModalRef"
    :selectList="typeList"
    @isOk="getList(true, true)"
  />
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue'
import { getDict } from '@/api/dict'

import { LeftOutlined } from '@ant-design/icons-vue'

import IndexDetailModal from './IndexDetailModal.vue'
import SerchButtom from '@/components/YuanQi-SearchButtom.vue'

import { cameraList } from './serve.api'

const emit = defineEmits(['change', 'isOk'])
const handleBack = () => emit('change')

const dataSource = ref()
const loading = ref<boolean>(false)
const paginationRef = ref()
const statusList = ref<any[]>([])
const typeList = ref<any[]>([])

const indexDetailModalRef = ref()

let searchFormClone = {
  pageNum: 1,
  pageSize: 10,
  name: '',
  status: undefined,
  type: undefined
}

// 搜索参数
const searchForm = reactive({
  name: '',
  status: undefined,
  type: undefined
})

const columns = ref([
  {
    title: '序号',
    dataIndex: 'order',
    width: '7.5rem'
  },
  {
    title: 'NVR名称',
    dataIndex: 'nvrName',
    key: 'nvrName',
    ellipsis: true
  },
  {
    title: '摄像机名称',
    dataIndex: 'name',
    key: 'name',
    ellipsis: true
  },
  {
    title: '摄像机类型',
    dataIndex: 'typeName',
    key: 'typeName',
    ellipsis: true
  },
  {
    title: '摄像机ID',
    dataIndex: 'modelCameraId',
    key: 'modelCameraId',
    ellipsis: true
  },
  {
    title: '通道号',
    dataIndex: 'passNum',
    key: 'passNum',
    ellipsis: true
  },
  {
    title: '状态',
    dataIndex: 'statusName',
    key: 'statusName',
    ellipsis: true
  },
  {
    title: '操作',
    dataIndex: 'operation',
    key: 'operation',
    ellipsis: true,
    width: '12rem'
  }
])

// 重置
const reset = () => {
  searchForm.name = ''
  searchForm.status = undefined
  searchForm.type = undefined
  getList(true, true)
}

const pagination = reactive({
  nvrId: '',
  total: 0,
  pageSize: 10, // 每页中显示10条数据
  position: ['bottomCenter'],
  current: 1,
  showSizeChanger: true,
  showQuickJumper: true,
  showTotal: (total: number) => `共 ${total} 条`
})

// 初始化数据
const getList = (flag = false, isSearch = false) => {
  if (flag) {
    pagination.current = 1
    paginationRef.value.changeCurrent(pagination.current)
  }
  loading.value = true
  dataSource.value = []

  const params = {
    nvrId: pagination.nvrId,
    name: searchForm.name,
    status: searchForm.status,
    type: searchForm.type,
    pageNum: pagination.current,
    pageSize: pagination.pageSize
  }
  if (isSearch) {
    searchFormClone = JSON.parse(JSON.stringify(params))
  } else {
    params.name = searchFormClone.name
    params.status = searchFormClone.status
    params.type = searchFormClone.type
  }

  cameraList(params)
    .then((res) => {
      if (res) {
        console.log(res, '摄像机列表')

        dataSource.value = res.list
        pagination.total = res.total

        paginationRef.value.changeTotal(pagination.total)
        loading.value = false
      } else {
        loading.value = false
      }
    })
    .catch(() => {
      loading.value = false
    })
}

const getListByPage = (page: number, pageSize: number) => {
  if (pagination.current !== page) {
    pagination.current = page
    getList()
  }
  if (pagination.pageSize !== pageSize) {
    pagination.pageSize = pageSize
    getList(true)
  }
}

// 新增/编辑/删除操作
type ModalType = 'add' | 'edit' | 'delete'

const openModal = (type: ModalType, data?: any) => {
  indexDetailModalRef.value.showModal(type, data)
}

function transferData(vals: any) {
  pagination.nvrId = vals.id
  getList(true, true)
}

onMounted(() => {
  getDict('nvr_camera_status').then((res) => {
    statusList.value = res.map((item: any) => ({
      label: item.dictLabel,
      value: item.dictValue
    }))
  })

  getDict('camera_type').then((res) => {
    typeList.value = res.map((item: any) => ({
      label: item.dictLabel,
      value: item.dictValue
    }))
  })
})

defineExpose({ transferData })
</script>
<style lang="less" scoped>
._input {
  width: 13.125rem;
  vertical-align: middle;

  &:not(:first-child) {
    margin-left: 1.25rem;
  }
}

.back {
  display: flex;
  align-items: center;

  span {
    font-size: 0.875rem;
    text-decoration: none;
    color: #86909c;
  }
}
</style>
